<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7 0007
 * Time: 17:16
 */
namespace app\index\model;
class Message
{
    static function send($data,$user){
        $st = "'data:$data,to:$user'";
        $str = '<script>
ws = new WebSocket("tcp://127.0.0.1:4646");
    ws.onopen = function() {
        ws.send('.$st.');
    };
</script>';
        echo $str;
        return true;
    }
    static function sendTo($data,$user){
        $st = "data:$data,to:$user'";
        $obj = stream_socket_client("tcp://127.0.0.1:4646",$errno,$errstr,60);
        if(!$obj){
            return false;
        }
        $str = stream_socket_sendto($obj,$st,STREAM_OOB);
        fclose($obj);
        return $str;
    }
}